package com.atcx.service.impl;

import com.atcx.mapper.ClassesMapper;
import com.atcx.mapper.NoticesMapper;
import com.atcx.mapper.StudentClassMapper;
import com.atcx.pojo.Notices;
import com.atcx.service.NoticesService;
import com.atcx.util.PageResult;
import com.atcx.util.QueryPageBean;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author pangShu
 * @since 2022-03-23
 */
@Service
public class NoticesServiceImpl extends ServiceImpl<NoticesMapper, Notices> implements NoticesService {


    @Resource
     private NoticesMapper noticesMapper;
    @Resource
    private StudentClassMapper studentClassMapper;
    @Resource
    private ClassesMapper  classesMapper;
    /**
     * @Author:pangshu
     * @Description: 添加公告信息1
     * @Date:21:45 2022/3/23
     * @Param:
     * @return: int
     **/
    @Override
    public int addNotices(Notices notices) {
        //添加时间
        notices.setCreatetime(LocalDate.now());
        int insert = noticesMapper.insert(notices);
        return insert;
    }

    @Override
    public int updateNotices(Notices notices) {
        int i = noticesMapper.updateById(notices);
        return i;
    }

    @Override
    public int deleteNotices(int id) {
        int i = noticesMapper.deleteById(id);
        return i;
    }

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        Page<Notices> page = new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        QueryWrapper<Notices> wrapper = new QueryWrapper<>();
        if (queryPageBean.getQueryString() != null&&queryPageBean.getQueryString().trim().length()!=0){
            //有查询条件就添加查询条件
            String trim = queryPageBean.getQueryString().trim();
            wrapper.eq("publish",trim);
        }
        Page<Notices> noticesPage = noticesMapper.selectPage(page, wrapper);
        PageResult pageResult = new PageResult(noticesPage.getTotal(), noticesPage.getRecords());
        return pageResult;
    }

    @Override
    public Notices getNotice() {
        List<Notices> notices = noticesMapper.selectList(new QueryWrapper<Notices>());
        return notices.get(0);
    }

    @Override
    public PageResult findByPageWithUser(QueryPageBean queryPageBean) {
        //根据发布人姓名查询相应公告
        PageResult byPage = findByPage(queryPageBean);
        return byPage;
    }

    @Override
    public Notices getNoticeByUser(String name) {
        QueryWrapper<Notices> wrapper = new QueryWrapper<>();
        wrapper.eq("publish",name);
        Notices notices = noticesMapper.selectOne(wrapper);
        return notices;
    }

    @Override
    public Notices getNoticeByClass(String name) {
        Notices notice = getNoticeByUser(name);
        return notice;
    }

}
